package com.blog.system.api.feign;

import cn.hutool.extra.spring.SpringUtil;
import com.blog.common.core.utils.AddressUtils;
import com.blog.common.core.utils.SpringUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import static com.blog.common.core.constant.ThirdUrl.IP_URL;

/**
 * <p>
 * ip地址服务
 * </p>
 *
 * @author Vivian
 * @date 2023/7/2
 **/
@FeignClient(contextId = "RemoteAddressService", name = "RemoteAddressService", url = IP_URL)
public interface RemoteAddressService {

    /**
     * 根据ip获取地址
     *
     * @param ip ip地址
     * @param flag 是否json返回
     * @return {@link AddressUtils.IpEntity}
     */
    @GetMapping("/")
    AddressUtils.IpEntity getAddressByIp(@RequestParam("ip") String ip, @RequestParam("json")Boolean flag);

    /**
     * 根据ip获取地址
     *
     * @param ip ip地址
     * @return {@link AddressUtils.IpEntity}
     */
    default AddressUtils.IpEntity getAddressByIp(String ip){
        RemoteAddressService remoteAddressService = SpringUtil.getBean(this.getClass());
        return remoteAddressService.getAddressByIp(ip, true);
    }
}
